package cn.wolfcode.shiro;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CrmRealm extends AuthorizingRealm {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher){
           super.setCredentialsMatcher(credentialsMatcher);
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
      //通过tolen获取到用户名
         UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken)token;
        String username = usernamePasswordToken.getUsername();
        //查询数据库中是否存在该用户名
        Employee employee = employeeMapper.selectByName(username);
        if (employee!=null){
            if(!employee.isStatus()){
               throw new RuntimeException("账号被禁用");
            }
            //如果存在.就把用户的用户名,密码封装到AuthorizationInfo中
            //第一个参数可以定义为当前用户登录的对像,提供给其他地方使用,第二个参数是用户真正的密码，
            //第三个参数是盐, 第四个参数是当前数据源的名字
            return new SimpleAuthenticationInfo(employee,employee.getPassword(), ByteSource.Util.bytes(employee.getName()),this.getName());
        }
        return null;
    }


    /*
    * 提供授权信息数据(当前用户拥有)
    * */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取当前用户登录对象
//        Subject subject = SecurityUtils.getSubject();
//        Employee employee = (Employee)subject.getPrincipal();
        //框架有提供参数，直接拿来用即可
        Employee employee = (Employee)principalCollection.getPrimaryPrincipal();
        //info对象中的数据就是当前用户的权限和角色的信息
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
       //判断当前登录用户是不是管理员
        if(!employee.isAdmin()){
            //根据员工的id查询员工的角色编码合集
            List<String> roles = roleMapper.selectSnByEmpId(employee.getId());
            //将用户拥有的角色添加到授权信息对象中，供Shiro权限校验使用
            info.addRoles(roles);
            //根据用户id查询员工拥有的权限表达式，供shiro权限校验使用
            List<String> expressions = permissionMapper.selectByElId(employee.getId());
            //将用户拥有的权限添加到授权信息对象中，供Shiro权限校验使用
            info.addStringPermissions(expressions);
        }else{
            info.addStringPermission("*:*");//权限通配符
            info.addRole("ADMIN");   //管理员角色,可以给也可以不给
        }
        return info;
    }

}
